package com.graduation.mybatis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.mybatis.bean.News;
import com.graduation.mybatis.bean.NewsDTO;
import com.graduation.mybatis.bean.NewsListDTO;
import com.graduation.mybatis.mapper.UserLikeMapper;
import com.graduation.mybatis.service.NewsService;
import com.graduation.mybatis.mapper.NewsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 23726
* @description 针对表【news】的数据库操作Service实现
* @createDate 2025-05-01 16:18:02
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News>
    implements NewsService{
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private UserLikeMapper userLikeMapper;

    public NewsDTO getNewsDetail(Long newsId, Long userId) {
        News news = newsMapper.selectById(newsId);
        List<String> images = newsMapper.selectImagesByNewsId(newsId);

        Integer commentCount = userLikeMapper.countLikes(newsId);
        Boolean isLiked = (userId != null) ? userLikeMapper.isUserLiked(userId, newsId) == 1 : false;


        return new NewsDTO(
                news.getId(),
                news.getTitle(),
                news.getContent(),
                news.getCategory(),
                news.getPublishTime(),
                images,
                commentCount,
                isLiked
        );
    }

    public List<NewsListDTO> getNewsList(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        return newsMapper.selectNewsList(offset, pageSize);
    }
}




